package com.gljx.web.controller;

import com.gljx.util.chyxion.xls.TableToXls;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Scanner;

/**
 * Created by Administrator on 2017/12/13 0013.
 */
@Controller
@RequestMapping("/")
public class InputXls {
    @Autowired
    HttpSession session;
    /**
     * @Author lcx
     * @Date 2017/12/13 0013 上午 10:41
     * 导出xls
     */
    @RequestMapping(value = "/outXls")
    public void outXls(String xls, HttpServletResponse response,String fileNames) throws Exception {
        String fileName = fileNames+".xls";
        fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName);// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel");
        /*response.setHeader("Pragma", "no-cache");*/
       /* response.setHeader("Cache-Control", "no-cache");*/
        response.setDateHeader("Expires", 0);

        OutputStream outputStream = response.getOutputStream();
        TableToXls.process(xls, outputStream);
        outputStream.flush();
        outputStream.close();

    }

    @RequestMapping(value = "/outXlsByjson")
    @ResponseBody
    public boolean outXlsByjson(@RequestParam(defaultValue = "", value = "xls") String xls,
                                @RequestParam(defaultValue = "", value = "fileNames") String fileNames) throws Exception {
        HttpServletResponse response = (HttpServletResponse) session.getAttribute("response");
        String fileName = fileNames + ".xls";
        fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName);// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        OutputStream outputStream = response.getOutputStream();
        TableToXls.process(xls, outputStream);
        outputStream.flush();
        outputStream.close();

        return true;

    }

}
